package base;

import java.awt.Color;
import java.awt.event.*;
import java.util.ArrayList;

public class SolverGrid extends Grid {
        private boolean keyTrue=false;
	public SolverGrid() {
	}

	public SolverGrid(Crossword cw) {
		super(cw);
	}

	public void init() {
		// add a mouse listener to every square
		for (int i = 0; i < squares.size(); i++) {
			Square s = (Square) squares.get(i);
			s.setLetter(" ");
			s.addMouseListener(new MListener());
		}
		this.addKeyListener(new KListener());
		requestFocus();
	}

//	@Override
//	public void setSelectedWord(Word w) {
//		selectedWord = w;
//		selectedSquare = findSquare(w.getY(), w.getX());
//		validate();
//	}

	public void hintWord() {
		for (int i = 0; i < squares.size(); i++) {
			Square s = (Square) squares.get(i);
			Word wd = s.getWordDown();
			Word wa = s.getWordAcross();
			Word wb1 = s.getWordBias1();
			Word wb2 = s.getWordBias2();
			if (wd == selectedWord) {
				if (wd.getWordDirection() == Word.ACROSS) {
					String answerLetter = String.valueOf(wd.getWord().charAt(
							s.getLetterIndexAcross()));
					s.setLetter(answerLetter);
					s.setIsHint(true);
				} else if (wd.getWordDirection() == Word.DOWN) {
					String answerLetter = String.valueOf(wd.getWord().charAt(
							s.getLetterIndexDown()));
					s.setLetter(answerLetter);
					s.setIsHint(true);
				} else if (wd.getWordDirection() == Word.BIAS1) {
					String answerLetter = String.valueOf(wd.getWord().charAt(
							s.getLetterIndexBias1()));
					s.setLetter(answerLetter);
					s.setIsHint(true);
				} else if (wd.getWordDirection() == Word.BIAS2) {
					String answerLetter = String.valueOf(wd.getWord().charAt(
							s.getLetterIndexBias2()));
					s.setLetter(answerLetter);
					s.setIsHint(true);
				}
			} else if (wa == selectedWord) {
				if (wa.getWordDirection() == Word.ACROSS) {
					String answerLetter = String.valueOf(wa.getWord().charAt(
							s.getLetterIndexAcross()));
					s.setLetter(answerLetter);
					s.setIsHint(true);
				} else if (wa.getWordDirection() == Word.DOWN) {
					String answerLetter = String.valueOf(wa.getWord().charAt(
							s.getLetterIndexDown()));
					s.setLetter(answerLetter);
					s.setIsHint(true);
				} else if (wa.getWordDirection() == Word.BIAS1) {
					String answerLetter = String.valueOf(wa.getWord().charAt(
							s.getLetterIndexBias1()));
					s.setLetter(answerLetter);
					s.setIsHint(true);
				} else if (wa.getWordDirection() == Word.BIAS2) {
					String answerLetter = String.valueOf(wa.getWord().charAt(
							s.getLetterIndexBias2()));
					s.setLetter(answerLetter);
					s.setIsHint(true);
				}
			} else if (wb1 == selectedWord) {
				if (wb1.getWordDirection() == Word.ACROSS) {
					String answerLetter = String.valueOf(wb1.getWord().charAt(
							s.getLetterIndexAcross()));
					s.setLetter(answerLetter);
					s.setIsHint(true);
				} else if (wb1.getWordDirection() == Word.DOWN) {
					String answerLetter = String.valueOf(wb1.getWord().charAt(
							s.getLetterIndexDown()));
					s.setLetter(answerLetter);
					s.setIsHint(true);
				} else if (wb1.getWordDirection() == Word.BIAS1) {
					String answerLetter = String.valueOf(wb1.getWord().charAt(
							s.getLetterIndexBias1()));
					s.setLetter(answerLetter);
					s.setIsHint(true);
				} else if (wb1.getWordDirection() == Word.BIAS2) {
					String answerLetter = String.valueOf(wb1.getWord().charAt(
							s.getLetterIndexBias2()));
					s.setLetter(answerLetter);
					s.setIsHint(true);
				}
			} else if (wb2 == selectedWord) {
				if (wb2.getWordDirection() == Word.ACROSS) {
					String answerLetter = String.valueOf(wb2.getWord().charAt(
							s.getLetterIndexAcross()));
					s.setLetter(answerLetter);
					s.setIsHint(true);
				} else if (wb2.getWordDirection() == Word.DOWN) {
					String answerLetter = String.valueOf(wb2.getWord().charAt(
							s.getLetterIndexDown()));
					s.setLetter(answerLetter);
					s.setIsHint(true);
				} else if (wb2.getWordDirection() == Word.BIAS1) {
					String answerLetter = String.valueOf(wb2.getWord().charAt(
							s.getLetterIndexBias1()));
					s.setLetter(answerLetter);
					s.setIsHint(true);
				} else if (wb2.getWordDirection() == Word.BIAS2) {
					String answerLetter = String.valueOf(wb2.getWord().charAt(
							s.getLetterIndexBias2()));
					s.setLetter(answerLetter);
					s.setIsHint(true);
				}
			}
			//s.validate();
			//s.repaint();
		}
	}

	public void showSolution() {
		for (int i = 0; i < squares.size(); i++) {
			Square s = (Square) squares.get(i);
			Word w = s.getWord();
			if (w != null) {
				w.setIsEditable(false);
				if (w.getWordDirection() == Word.ACROSS) {
					String answerLetter = String.valueOf(w.getWord().charAt(
							s.getLetterIndexAcross()));
					s.setLetter(answerLetter);
					s.setIsHint(false);
				} else if (w.getWordDirection() == Word.DOWN) {
					String answerLetter = String.valueOf(w.getWord().charAt(
							s.getLetterIndexDown()));
					s.setLetter(answerLetter);
					s.setIsHint(false);
				} else if (w.getWordDirection() == Word.BIAS1) {
					String answerLetter = String.valueOf(w.getWord().charAt(
							s.getLetterIndexBias1()));
					s.setLetter(answerLetter);
					s.setIsHint(false);
				} else if (w.getWordDirection() == Word.BIAS2) {
					String answerLetter = String.valueOf(w.getWord().charAt(
							s.getLetterIndexBias2()));
					s.setLetter(answerLetter);
					s.setIsHint(false);
				}
			}
			s.validate();
			//s.repaint();
		}
	}
        public boolean  checkClientIsCorect(String s){
            if (selectedSquare != null) {
            Word w = selectedWord;
                if (!w.getIsChecked()){
                    return (w.getWord().equalsIgnoreCase(s));
                }
            }
            return false;
        }
	public boolean checkIsCorrect() {
		if (selectedSquare != null) {
				Word w = selectedWord;
				if ((!w.getIsChecked() && !w.getIsHint())||(!w.getIsChecked() && w.getIsHint()) && w == keyWord) {
					w.setIsChecked(true);
					w.setIsEditable(false);
					if (w.getWordDirection() == Word.ACROSS) {
						for (int i = w.getX(); i < w.getX() + w.getLength(); i++) {
							Square s = findSquare(w.getY(), i);
							String answerLetter = String.valueOf(w.getWord()
									.charAt(s.getLetterIndexAcross()));
							if (!s.getLetter().equals(
									answerLetter.toUpperCase())) {
								if (w == keyWord) {
									paint(Color.DARK_GRAY, w);
								} else
									paint(Color.gray, w);
								return false;
							}
						}
					} else if (w.getWordDirection() == Word.DOWN) {

						for (int i = w.getY(); i < w.getY() + w.getLength(); i++) {
							Square s = findSquare(i, w.getX());
							String answerLetter = String.valueOf(w.getWord()
									.charAt(s.getLetterIndexDown()));
							if (!s.getLetter().equals(
									answerLetter.toUpperCase())) {
								if (w == keyWord) {
									paint(Color.DARK_GRAY, w);
								} else
									paint(Color.gray, w);
								return false;
							}
						}
					} else if (w.getWordDirection() == Word.BIAS1) {

						for (int i = w.getY(); i < w.getY() + w.getLength(); i++) {
							Square s = findSquare(i, w.getX() + i - w.getY());
							String answerLetter = String.valueOf(w.getWord()
									.charAt(s.getLetterIndexBias1()));
							if (!s.getLetter().equals(
									answerLetter.toUpperCase())) {
								if (w == keyWord) {
									paint(Color.DARK_GRAY, w);
								} else
									paint(Color.gray, w);
								return false;
							}
						}
					} else if (w.getWordDirection() == Word.BIAS2) {

						for (int i = w.getX(); i < w.getX() + w.getLength(); i++) {
							Square s = findSquare(w.getY() + w.getX() - i, i);
							String answerLetter = String.valueOf(w.getWord()
									.charAt(s.getLetterIndexBias2()));
							if (!s.getLetter().equals(
									answerLetter.toUpperCase())) {
								if (w == keyWord) {
									paint(Color.DARK_GRAY, w);
								} else
									paint(Color.gray, w);
								return false;
							}
						}
					}
					if (w == keyWord) {
						paint(Color.MAGENTA, w);
					} else
						paint(Color.cyan, w);
					return true;
				}
			
		}
		return false;

	}
        public int checkAfterFinish() {
            int a=0;
            ArrayList al=crossword.getWords();
            for(int b=0;b<al.size();b++)
            {
                    Word w=(Word) al.get(b);
                  //  int test=0;
                    if ((!w.getIsChecked() && !w.getIsHint())||(!w.getIsChecked() && w.getIsHint()) && w == keyWord) {
                            w.setIsChecked(true);
                            w.setIsEditable(false);
                            if (w.getWordDirection() == Word.ACROSS) {
                                    for (int i = w.getX(); i < w.getX() + w.getLength(); i++) {
                                            Square s = findSquare(w.getY(), i);
                                            String answerLetter = String.valueOf(w.getWord()
                                                            .charAt(s.getLetterIndexAcross()));
                                            if (!s.getLetter().toUpperCase().equals(
                                                            answerLetter.toUpperCase())) {
                                                    if (w == keyWord) {
                                                            paint(Color.DARK_GRAY, w);
                                                    } else
                                                            paint(Color.gray, w);
                                           //          test=1;
                                            //         break;

                                            }
                                           
                                    }
                            } else if (w.getWordDirection() == Word.DOWN) {

                                    for (int i = w.getY(); i < w.getY() + w.getLength(); i++) {
                                            Square s = findSquare(i, w.getX());
                                            String answerLetter = String.valueOf(w.getWord()
                                                            .charAt(s.getLetterIndexDown()));
                                            if (!s.getLetter().toUpperCase().equals(
                                                            answerLetter.toUpperCase())) {
                                                    if (w == keyWord) {
                                                            paint(Color.DARK_GRAY, w);
                                                    } else
                                                            paint(Color.gray, w);
                                                //      test=1;
                                               //      break;

                                            }
                                          
                                    }
                            } else if (w.getWordDirection() == Word.BIAS1) {

                                    for (int i = w.getY(); i < w.getY() + w.getLength(); i++) {
                                            Square s = findSquare(i, w.getX() + i - w.getY());
                                            String answerLetter = String.valueOf(w.getWord()
                                                            .charAt(s.getLetterIndexBias1()));
                                            if (!s.getLetter().toUpperCase().equals(
                                                            answerLetter.toUpperCase())) {
                                                    if (w == keyWord) {
                                                            paint(Color.DARK_GRAY, w);
                                                    } else
                                                            paint(Color.gray, w);
                                           //         test=1;
                                            //        break;

                                            }
                                            
                                    }
                            } else if (w.getWordDirection() == Word.BIAS2) {

                                    for (int i = w.getX(); i < w.getX() + w.getLength(); i++) {
                                            Square s = findSquare(w.getY() + w.getX() - i, i);
                                            String answerLetter = String.valueOf(w.getWord()
                                                            .charAt(s.getLetterIndexBias2()));
                                            if (!s.getLetter().toUpperCase().equals(
                                                            answerLetter.toUpperCase())) {
                                                    if (w == keyWord) {
                                                            paint(Color.DARK_GRAY, w);

                                                    } else
                                                            paint(Color.gray, w);
                                         //   test=1;
                                          //  break;
                                            }
                                           
                                    }
                            }
//                            if(test==0)
//                            {
//                                if (w == keyWord)
//                                {
//                                    paint(Color.MAGENTA, w);
//                                    a+=100;
//                                    keyTrue=true;
//                                }
//                                else
//                                {
//                                        switch (w.getClueUsed())
//                                        {
//                                            case 1:
//                                                    a += 15;
//                                                   break;
//                                            case 2:
//                                                    a += 10;
//                                                   break;
//                                            case 3:
//                                                    a += 5;
//
//                                        }
//                                        paint(Color.cyan, w);
//                                }
//                            }
                    }

            }
           
            return a;
	}
        public boolean getKeyTrue()
        {
            return keyTrue;
        }
	class MListener implements MouseListener {
		public void mouseClicked(MouseEvent e) {
                        requestFocus();
			selectedSquare = (Square) e.getSource();
			selectedWord = selectedSquare.getWord();
			
			validate();
		}

		public void mouseEntered(MouseEvent e) {
		}

		public void mouseExited(MouseEvent e) {
		}

		public void mousePressed(MouseEvent e) {
		}

		public void mouseReleased(MouseEvent e) {
		}

	}

	class KListener implements KeyListener {
		public void keyPressed(KeyEvent e) {
		}

		public void keyReleased(KeyEvent e) {
		}

		public void keyTyped(KeyEvent e) {
			String s = null;
			if (selectedWord.getIsEditable()) {
				if (e.getKeyChar() == '\b')
					selectedSquare.setLetter(" ");
				// else if(e.getKeyChar() == '\n')
				// {
				// checkIsCorrect();
				// selectedWord.setIsChecked(true);
				// }
				else
			             selectedSquare.setLetter(String.valueOf(e.getKeyChar()));
//                              if (selectedSquare.getWord().getWord().length() == findEndSquare(selectedSquare))
//                                    checkIsCorrect();
				if (e.getKeyChar() != '\b')
					selectedSquare = getNextSquare();
				else
					selectedSquare = getPreviousSquare();
				validate();
			}

		}
	}
}
